namespace Defiant.SqlServer.PowerShellScriptTask
{
    using System;
    using System.Collections.Generic;
    using System.Management.Automation.Host;
    using Microsoft.SqlServer.Dts.Runtime;

    internal class ScriptHostInterface : PSHostUserInterface
    {
        private readonly IDTSComponentEvents componentEvents;
        private readonly ScriptHostInterfaceRaw rawUi = new ScriptHostInterfaceRaw();

        internal ScriptHostInterface(IDTSComponentEvents componentEvents)
        {
            this.componentEvents = componentEvents;
        }

        public override PSHostRawUserInterface RawUI
        {
            get { return this.rawUi; }
        }

        public override Dictionary<string, System.Management.Automation.PSObject> Prompt(string caption, string message, System.Collections.ObjectModel.Collection<FieldDescription> descriptions)
        {
            throw new NotImplementedException("The method is not implemented.");
        }

        public override int PromptForChoice(string caption, string message, System.Collections.ObjectModel.Collection<ChoiceDescription> choices, int defaultChoice)
        {
            throw new NotImplementedException("The method is not implemented.");
        }

        public override System.Management.Automation.PSCredential PromptForCredential(string caption, string message, string userName, string targetName, System.Management.Automation.PSCredentialTypes allowedCredentialTypes, System.Management.Automation.PSCredentialUIOptions options)
        {
            throw new NotImplementedException("The method is not implemented.");
        }

        public override System.Management.Automation.PSCredential PromptForCredential(string caption, string message, string userName, string targetName)
        {
            throw new NotImplementedException("The method is not implemented.");
        }

        public override string ReadLine()
        {
            throw new NotImplementedException("The method is not implemented.");
        }

        public override System.Security.SecureString ReadLineAsSecureString()
        {
            throw new NotImplementedException("The method is not implemented.");
        }

        public override void Write(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string value)
        {
            this.Write(value);
        }

        public override void Write(string value)
        {
            bool fireAgain = true;
            this.componentEvents.FireInformation(0, Constants.COMPONENT, value, string.Empty, 0, ref fireAgain);
        }

        public override void WriteDebugLine(string message)
        {
            this.Write("DEBUG: " + message);
        }

        public override void WriteErrorLine(string value)
        {
            this.componentEvents.FireError(0, Constants.COMPONENT, value, string.Empty, 0);
        }

        public override void WriteLine(string value)
        {
            this.Write(value);
        }

        public override void WriteProgress(long sourceId, System.Management.Automation.ProgressRecord record)
        {
            bool fireAgain = true;
            this.componentEvents.FireProgress(record.StatusDescription, record.PercentComplete, 0, 100, Constants.COMPONENT, ref fireAgain);
        }

        public override void WriteVerboseLine(string message)
        {
            this.Write("VERBOSE: " + message);
        }

        public override void WriteWarningLine(string message)
        {
            this.componentEvents.FireWarning(0, Constants.COMPONENT, message, string.Empty, 0);
        }
    }
}
